package ru.nsu.fit.ask_joe.user_interface_manager.realization;

import ru.nsu.fit.ask_joe.MainManager;
import ru.nsu.fit.ask_joe.R;
import ru.nsu.fit.ask_joe.user_interface_manager.interfaces.IPanel;
import ru.nsu.fit.client.core.response.*;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegistrationPanel implements IPanel {

private static final int resourceId = R.layout.authentication;
	
	private final View _view;
	private final TextView _textRegistration;
	private final EditText _editLogin, _editPass;
	private final Button _buttonNext, _buttonBack;
	private ProgressDialog _progressDialog;
	
	private class ResponseProcessor extends KernelResponseProcessor {

		@Override
		protected void onPostExecute(KernelResponse result) {
			super.onPostExecute(result);
            if (_progressDialog != null)
            {
                _progressDialog.dismiss();
                _progressDialog = null;
            }
            if (result.isSuccessful())
			{
                long sessionId = result.getUserSessionId();
				MainManager.getUserInterfaceManager().setSessionId(sessionId);
                MainManager.getUserInterfaceManager().setCurrentPanel(new AuthenticationPanel());
			}
			else
			{
				AlertDialog.Builder builder =  new AlertDialog.Builder(getView().getContext());
				builder.setTitle(getView().getContext().getString(R.string.Message_RegistrationError));
				builder.setMessage(result.getErrorText()); //TODO translate error message into current language
				builder.setPositiveButton(getView().getContext().getString(R.string.Button_Ok), null);
				builder.show();
			}
		}
	}
	
	public RegistrationPanel()
	{
		_view = MainManager.getUserInterfaceManager().getMainActivity().getLayoutInflater().inflate(resourceId, null);
		
		_editLogin = (EditText)_view.findViewById(R.id.edit_login);
		_editPass = (EditText)_view.findViewById(R.id.edit_pass);
		
		_buttonNext = (Button)_view.findViewById(R.id.button_next);
		_buttonNext.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onNextClick(v);
			}
		});

        _buttonBack = (Button)_view.findViewById(R.id.button_back);
        _buttonBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackClick(v);
            }
        });

        _textRegistration = (TextView)_view.findViewById(R.id.text_registration);
		_textRegistration.setVisibility(View.INVISIBLE);

        _view.findViewById(R.id.text_ip).setVisibility(View.INVISIBLE);
        _view.findViewById(R.id.edit_ip).setVisibility(View.INVISIBLE);
    }
	
	private void onNextClick(View v) 
	{
		try {
			final int id = MainManager.getClientKernel().registration(_editLogin.getText().toString(), _editPass.getText().toString());
			_progressDialog = ProgressDialog.show(v.getContext(), v.getContext().getString(R.string.Message_PleaseWait), v.getContext().getString(R.string.Message_Registration));
			
			new ResponseProcessor().execute(id);
		} catch (Exception e) {
			MainManager.getLogManager().getLogger().printError(e.getMessage());
		}
	}

    private void onBackClick(View v)
    {
        MainManager.getUserInterfaceManager().setCurrentPanel(new AuthenticationPanel());
    }

    @Override
	public View getView() {
		return _view;
	}
}
